package com.cn.intelligence.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.cn.intelligence.pojo.EUDataGridResult;
import com.cn.intelligence.pojo.TbMUser;
import com.cn.intelligence.service.LoginService;

@Controller
public class LoginController {

	@Autowired
	public LoginService loginService;
	@Autowired
	HttpServletRequest request;
	/**
	 * 用户登录
	 * @param username
	 * @param password
	 * @return
	 */
	
	@RequestMapping("tologin")
	public String checkUserInfoTologin(String username, String password, RedirectAttributes model){
		// 验证用户是否存在
		TbMUser tbMuser=new TbMUser();
		tbMuser.setUsername(username);
		tbMuser.setPassword(password);
		TbMUser user = loginService.getUserInfo(tbMuser);
		if(user!=null){
			request.getSession().setAttribute("login", true);
			request.getSession().setAttribute("role", user.getRole());
			request.getSession().setAttribute("user", user);

			return "redirect:/index";
		}else{
			 model.addFlashAttribute("msg", "该用户信息不正确，请重新输入");  
			return "redirect:/login";
		} 

	}
    /**
     * 查询书树节点信息
     * @param parentId
     * @return
     */
    @RequestMapping("/menu/list")
    @ResponseBody
    public List<Map<String,Object>> getMenuList(@RequestParam(value = "id",defaultValue = "0") Long parentId,String role){
    	List<Map<String,Object>> euTreeNodeList = loginService.getMenuList(parentId,role);
        return euTreeNodeList;
    }

}
